package com.user.query.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HtmlJsoup {

    public static String getHtmlResourceByURL(String url,String encoding) throws Exception {
        URL newurl=new URL(url);
        URLConnection connection=newurl.openConnection();
        StringBuffer stringBuffer=new StringBuffer();
        //网络流
        InputStreamReader isr=new InputStreamReader(connection.getInputStream(),encoding);
        BufferedReader bufferedReader=new BufferedReader(isr);
        String line=null;
        while ((line=bufferedReader.readLine())!=null) {
            stringBuffer.append(line+"\n");
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) throws Exception{
        String html=HtmlJsoup.getHtmlResourceByURL("https://www.jd.com","utf-8");
        System.out.println(html);
        Document document= Jsoup.connect("http://www.jb51.net/").get();
        Elements elements=document.getElementsByTag("img");
        for (Element element:elements) {
            String src=element.attr("src");
            if (!"".equals(src)&&null!=src&&src.startsWith("http://")) {
                System.out.println("开始下载.."+src);
                String filename="c:\\bgt\\2016";
                down(src,filename);
            }
        }
    }

    private static void down(String src, String filepath) {
        String filename=src.substring(src.lastIndexOf("/"));
        File files=new File(filepath);
        if (!files.exists()) {
            files.mkdirs();
        }
        try {
            URL url=new URL(src);
            HttpURLConnection huc=(HttpURLConnection)url.openConnection();
            InputStream is=huc.getInputStream();
            File file=new File(filepath+filename);
            FileOutputStream out=new FileOutputStream(file);
            int i=0;
            while ((i=is.read())!=-1) {
                out.write(i);
            }
            is.close();
            out.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
